package com.dangs.autotest.activity;

import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.view.Gravity;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.RecyclerView;

import com.dangs.autotest.R;
import com.dangs.autotest.service.FloatService;
import com.dangs.autotest.system.PlatformAPI;
import com.dangs.autotest.test.Task;
import com.dangs.autotest.test.TestManager;
import com.dangs.autotest.tool.L;
import com.dangs.autotest.ui.CheckListViewAdapter;
import com.dangs.autotest.ui.FloatView;

import java.util.ArrayList;
import java.util.List;

public class AutoTestActivity extends AppCompatActivity implements TestManager.TestManageListener {

    private static final String TAG = "AutoTestActivity";
    List<Task> tests;
    CheckListViewAdapter testAdapter;
    CheckListViewAdapter taskAdapter;
    private TestManager testManager;
    private CheckBox cbLocationUp, cbLocationDown, cbLocationCenter, cbLocationLeft, cbLocationRight;
    private CheckBox cbCpu, cbRam, cbFan, cbTask, cbProgress, cbMeanwhile;
    private TextView txVersion;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        testManager = TestManager.getInstance(this).setListener(this);
        requestWindowPermission();
        initUi();
    }

    void initUi() {
        tests = testManager.initTestsList();
        testAdapter = new CheckListViewAdapter(tests);
        RecyclerView testList = findViewById(R.id.list_test);
        testList.setAdapter(testAdapter);
        RecyclerView taskList = findViewById(R.id.list_task);
        taskAdapter = new CheckListViewAdapter(new ArrayList<>());
        taskList.setAdapter(taskAdapter);
        findViewById(R.id.bt_start).setOnClickListener(v -> startTest());;
        txVersion = findViewById(R.id.tx_version);
        txVersion.setText(PlatformAPI.getImp().getVersion(this, getPackageName()));
        cbMeanwhile = findViewById(R.id.checkbox_meanwhile);
        cbMeanwhile.setOnCheckedChangeListener(this::onConfigChanged);
        cbCpu = findViewById(R.id.cb_float_cpu);
        cbCpu.setOnCheckedChangeListener(this::onConfigChanged);
        cbFan = findViewById(R.id.cb_float_fan);
        cbFan.setOnCheckedChangeListener(this::onConfigChanged);
        cbRam = findViewById(R.id.cb_float_ram);
        cbRam.setOnCheckedChangeListener(this::onConfigChanged);
        cbTask = findViewById(R.id.cb_float_task);
        cbTask.setOnCheckedChangeListener(this::onConfigChanged);
        cbProgress = findViewById(R.id.cb_float_progress);
        cbProgress.setOnCheckedChangeListener(this::onConfigChanged);
        cbLocationUp = findViewById(R.id.cb_float_up);
        cbLocationUp.setOnCheckedChangeListener(this::onConfigChanged);
        cbLocationDown = findViewById(R.id.cb_float_down);
        cbLocationDown.setOnCheckedChangeListener(this::onConfigChanged);
        cbLocationLeft = findViewById(R.id.cb_float_left);
        cbLocationLeft.setOnCheckedChangeListener(this::onConfigChanged);
        cbLocationRight = findViewById(R.id.cb_float_right);
        cbLocationRight.setOnCheckedChangeListener(this::onConfigChanged);
        cbLocationCenter = findViewById(R.id.cb_float_center);
        cbLocationCenter.setOnCheckedChangeListener(this::onConfigChanged);
        FloatView.Config config = FloatView.Config.getConfigFromLocal(this);
        cbCpu.setChecked(config.isShowCpu);
        cbFan.setChecked(config.isShowFan);
        cbRam.setChecked(config.isShowRam);
        cbTask.setChecked(config.isShowTask);
        cbProgress.setChecked(config.isShowProgress);
//        cbLocationUp.setChecked((config.gravity & Gravity.TOP) != 0);
//        cbLocationDown.setChecked((config.gravity & Gravity.BOTTOM) != 0);
//        cbLocationLeft.setChecked((config.gravity & Gravity.LEFT) != 0);
//        cbLocationRight.setChecked((config.gravity & Gravity.RIGHT) != 0);
//        cbLocationCenter.setChecked((config.gravity & Gravity.CENTER) != 0);
    }

    void requestWindowPermission() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (!Settings.canDrawOverlays(this)) {
                try {
                    startActivityForResult(new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, Uri.parse("package:" + getPackageName())), 101);
                } catch (ActivityNotFoundException e) {
                    L.w(TAG, "no need request window permission");
                }
            } else {
                L.d(TAG, "have window permission");
            }
        }
    }

    private void startTest() {
        testManager.starTest(cbMeanwhile.isChecked());
        Intent service = new Intent(this, FloatService.class);
        FloatView.Config config = new FloatView.Config();
        service.putExtra(FloatView.Config.KEY, config);
        startService(service);
        finish();
    }


    public void onConfigChanged(CompoundButton buttonView, boolean isChecked) {
        L.d(TAG, "onConfigChanged:" + isChecked);
        switch (buttonView.getId()) {
            case R.id.cb_float_up:
            case R.id.cb_float_down:
            case R.id.cb_float_left:
            case R.id.cb_float_right:
            case R.id.cb_float_center:
                int gravity = Gravity.NO_GRAVITY;
                gravity |= cbLocationUp.isChecked() ? Gravity.TOP : Gravity.NO_GRAVITY;
                gravity |= cbLocationDown.isChecked() ? Gravity.BOTTOM : Gravity.NO_GRAVITY;
                gravity |= cbLocationLeft.isChecked() ? Gravity.LEFT : Gravity.NO_GRAVITY;
                gravity |= cbLocationRight.isChecked() ? Gravity.RIGHT : Gravity.NO_GRAVITY;
//                gravity |= cbLocationCenter.isChecked() ? Gravity.CENTER : Gravity.NO_GRAVITY;
                FloatView.Config.saveConfigIntoLocal(this, FloatView.Config.KEY_GRAVITY, gravity);
                break;
            case R.id.cb_float_cpu:
                FloatView.Config.saveConfigIntoLocal(this, FloatView.Config.KEY_SHOW_CPU, isChecked);
                break;
            case R.id.cb_float_ram:
                FloatView.Config.saveConfigIntoLocal(this, FloatView.Config.KEY_SHOW_RAM, isChecked);
                break;
            case R.id.cb_float_fan:
                FloatView.Config.saveConfigIntoLocal(this, FloatView.Config.KEY_SHOW_FAN, isChecked);
                break;
            case R.id.cb_float_progress:
                FloatView.Config.saveConfigIntoLocal(this, FloatView.Config.KEY_SHOW_PROGRESS, isChecked);
                break;
            case R.id.cb_float_task:
                FloatView.Config.saveConfigIntoLocal(this, FloatView.Config.KEY_SHOW_TASK, isChecked);
                break;
        }
    }

    @Override
    public void onUpdateTasks(Task task) {
        List<Task> tasks = task.getTasks();
        if (task.isChecked() && tasks != null && tasks.size() > 0) {
            taskAdapter.updateList(tasks).notifyDataSetChanged();
        }
    }

    @Override
    public void onUpdateTaskSettings(Task task) {
        L.d(TAG, "onUpdateTaskSettings:" + task);

    }
}
